package ioperations;

import book.*;

import java.util.Scanner;

public class AddOperation implements IOPeration {
    public void work(BookList bookList) {
        System.out.println("Add operation");

        int currentSize = bookList.getUsedSize();
        if(currentSize == bookList.getBooks().length){
            System.out.println("已经满了");
            return;
        }


        Scanner sc = new Scanner(System.in);
        System.out.println("书名：");
        String bookName = sc.nextLine();
        System.out.println("作者：");
        String author = sc.nextLine();
        System.out.println("类型：");
        String type = sc.nextLine();
        System.out.println("价格");
        int price = sc.nextInt();

        Book newbook = new Book(bookName, author, price, type);

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(bookName)) {
                System.out.println("有这本书不能插入！");
                return;
            }
        }

        bookList.setBook(currentSize,newbook);
        bookList.setUsedSize(currentSize+1);

        System.out.println("新增成功");


    }



}
